package org.example.order.service;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.example.order.entity.Order;
import org.example.order.mapper.OrderMapper;
import org.example.order.rpc.InventoryClient;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class OrderService {
    private final OrderMapper orderMapper;
    private final InventoryClient inventoryClient;

    @GlobalTransactional(rollbackFor = Exception.class)
    public String createOrder(String skuId, Integer quantity, Long userId) {
        String orderId = UUID.randomUUID().toString().replace("-", "");
        Order order = Order.builder()
                .orderId(orderId)
                .amount("100.00")
                .skuId(skuId)
                .userId(userId)
                .build();
        orderMapper.insert(order);
        inventoryClient.lockInventory(skuId, quantity);
        // 扣减优惠卷
        if (quantity % 2 == 0) {
            String str = null;
            str.equals("123");
        }
        return orderId;
    }

}
